#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re
t="abc123def456ghi*sdf3_abd"
print re.match("a",t).group()   #match只匹配开头 ==>a
'''
print help(re.match)
match(pattern, string, flags=0)
    Try to apply the pattern at the start of the string, returning
    a match object, or None if no match was found.
'''
print re.search("\d+",t).group()    #search就匹配第一个符合的返回==>123

print re.findall("\d+",t)   #返回所有匹配==》['123', '456', '3']

print re.split("[\d,\*]",t) #==>['abc', '', '', 'def', '', '', 'ghi', 'sdf', '_abd']

print re.sub("ab","cd",t,count=1)   #替换

name="zhang san"
res=re.search("(?P<name>\w+)\s(?P<lastname>\w+)",name)
print res.groups("name")
#==>('zhang', 'san')